package com.thti.mecool.client.command.result.plan;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.threeti.mecool.core.domain.model.plan.pojo.PlanJob;
import com.threeti.mecool.core.domain.model.plan.pojo.PlanProject;
import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.base.Cache;
import com.thti.mecool.client.base.HandlerConstant;
import com.thti.mecool.client.command.ICommand;
import com.thti.mecool.client.model.biz.PlanBizImpl;
import com.thti.mecool.client.protocol.plan.PlansProtocol;
import com.thti.mecool.client.protocol.plan.PlansProtocolReslut;
import com.thti.mecool.client.service.Protocol;
import com.thti.mecool.client.util.TimeUtil;

public class MyPlanCommand implements ICommand {
	PlanBizImpl planBizImpl;
	List<Map<String,Object[]>> list=new ArrayList<Map<String,Object[]>> (); 
	 

	@Override
	public void doCommand(Object o) {
		PlansProtocolReslut planProtocolResult = (PlansProtocolReslut) o;
		if (planProtocolResult != null) {
			planBizImpl=new PlanBizImpl(Cache.getmContext());
			if (planProtocolResult.getStatus() == 1 ){
				System.out.println("Ohhjajaja");
				if(planProtocolResult.getPlans().size()>0){
				for (int i = 0; i < planProtocolResult.getPlans().size(); i++) {
					if(planBizImpl.findPlanIdIsexist(planProtocolResult.getPlans().get(i).getPlanId(), String.valueOf(BaseActivity.getCurrentUser().getId())))
					planBizImpl.delete(planProtocolResult.getPlans().get(i).getPlanId(), false);
					list.add(savePlan(planProtocolResult.getPlans().get(i)));
				}
				if (null != list && list.size() > 0) {
					planBizImpl.savePlan(list);
				}
				}
				System.out.println("MyCommand_OK!");
				BaseActivity.setDataFlag(HandlerConstant.MyPlanCommand);
			}else{
				BaseActivity.setDataFlag(HandlerConstant.CommandFail);
			}
		}
	}

	

	public Map<String,Object[]> savePlan(PlansProtocol planServer) {
		Map<String,Object[]> map=new HashMap<String, Object[]>();
		map.put(HandlerConstant.PLAN_SQL,new Object[]{BaseActivity.getCurrentUser().getId(),planServer.getPlanId(),planServer.getName(), 
		         0,2,planServer.getPlanDate(),planServer.getStatus(),planServer.getDenyNotes(),planServer.getChangeNotes(),"",new Date()});
      return map;
	}

	public Map<String,Object[]> saveProject(PlanProject planProject) {
		Map<String,Object[]> map=new HashMap<String, Object[]>();
		map.put(HandlerConstant.PROJECT_SQL,new Object[]{BaseActivity.getCurrentUser().getId(),planProject.getProjectCode(),planProject.getProjectName(),
				planProject.getPlan().getId(),"2",new Date()});
		return map;
	}

	public Map<String,Object[]> saveTask(PlanJob planJob) {
		Map<String,Object[]> map=new HashMap<String, Object[]>();
		    map.put(HandlerConstant.TASK_SQL,new Object[]{BaseActivity.getCurrentUser().getId(),planJob.getPlanProject().getProjectCode(),
		    		planJob.getId(),planJob.getJob().getName(),TimeUtil.dateToString(planJob.getStartTime(),1),
		    		TimeUtil.dateToString(planJob.getEndTime(),1),planJob.getStoreCode(),planJob.getStoreName(),
		    		planJob.getPlanProject().getPlan().getId(),"local",0});
		return map;
	}

	
	@Override
	public String getCommandID() {
		// TODO Auto-generated method stub
		return Protocol.getAddListPlan;
	}

	@Override
	public Class<?> getCommandClass() {
		// TODO Auto-generated method stub
		return PlansProtocolReslut.class;
	}

}
